<?php

namespace App\HttpController;

use App\Service\ShopService;
use EasySwoole\HttpAnnotation\AnnotationTag\Api;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiDescription;
use EasySwoole\HttpAnnotation\AnnotationTag\ApiGroup;
use EasySwoole\HttpAnnotation\AnnotationTag\Method;
use EasySwoole\HttpAnnotation\AnnotationTag\Param;
use EasySwoole\ORM\Exception\Exception;
use Throwable;

/**
 * @ApiGroup(groupName="商城")
 */
class Shop extends BaseController
{
    /**
     * @Api(name="获取商城分类", path="/shop/getShopCate", version="1.0.0")
     * @ApiDescription(value="获取商城分类")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getShopCate(): bool
    {
        $res = ShopService::getInstance()->getShopCate();
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取商城分类下级", path="/shop/getShopCateS", version="1.0.0")
     * @ApiDescription(value="获取商城分类下级")
     * @Method(allow={POST})
     * @Param(name="nid", description="分类ID", required="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getShopCateS($nid): bool
    {
        $res = ShopService::getInstance()->getShopCateS($nid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取商城分类下产品", path="/shop/getGoodsList", version="1.0.0")
     * @ApiDescription(value="获取商城分类下产品")
     * @Method(allow={POST})
     * @Param(name="nid", description="分类ID", required="", from={JSON})
     * @Param(name="sort", defaultValue=0, description="排序方式：0.综合排序，1.销量优先，2.好评有限，3.价格最低，4.价格最高", required="", from={JSON})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getGoodsList($nid, $sort, $limit, $page): bool
    {
        $res = ShopService::getInstance()->getGoodsList($nid, $sort, $limit, $page);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取商品详情", path="/shop/getGoodDetail", version="1.0.0")
     * @ApiDescription(value="获取商品详情")
     * @Method(allow={POST})
     * @Param(name="gid", description="产品id", required="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getGoodDetail($gid): bool
    {
        $res = ShopService::getInstance()->getGoodDetail($gid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取商铺详情", path="/shop/getShopDetail", version="1.0.0")
     * @ApiDescription(value="获取商铺详情")
     * @Method(allow={POST})
     * @Param(name="sid", description="商铺id", required="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getShopDetail($sid): bool
    {
        $res = ShopService::getInstance()->getShopDetail($sid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取商户商城下产品", path="/shop/getShopGoodsList", version="1.0.0")
     * @ApiDescription(value="获取商户商城下产品")
     * @Method(allow={POST})
     * @Param(name="sid", description="商户ID", required="", from={JSON})
     * @Param(name="sort", defaultValue=0, description="排序方式：0.综合排序，1.销量优先，2.好评有限，3.价格最低，4.价格最高", required="", from={JSON})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @Param(name="nid", description="分类ID", defaultValue="0", required="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getShopGoodsList($sid, $sort, $limit, $page, $nid): bool
    {
        $res = ShopService::getInstance()->getShopGoodsList($sid, $sort, $limit, $page, $nid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="添加购物车", path="/shop/addCart", version="1.0.0")
     * @ApiDescription(value="添加购物车")
     * @Method(allow={POST})
     * @Param(name="sku", description="商品SKU", required="", from={JSON})
     * @Param(name="code", description="商品CODE", required="", from={JSON})
     * @Param(name="num", description="添加数量", required="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function addCart($sku, $code, $num): bool
    {
        $res = ShopService::getInstance()->addCart($this->uid, $sku, $code, $num);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的购物车", path="/shop/getCart", version="1.0.0")
     * @ApiDescription(value="获取我的购物车")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getCart(): bool
    {
        $res = ShopService::getInstance()->getCart($this->uid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="删除购物车单个", path="/shop/removeCart", version="1.0.0")
     * @ApiDescription(value="删除购物车单个")
     * @Method(allow={POST})
     * @Param(name="id", description="id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function removeCart($id): bool
    {
        $res = ShopService::getInstance()->removeCart($this->uid, $id);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="清空购物车全部", path="/shop/clearCart", version="1.0.0")
     * @ApiDescription(value="清空购物车全部")
     * @Method(allow={POST})
     * @Param(name="ids", description="ids", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function clearCart($ids): bool
    {
        $res = ShopService::getInstance()->clearCart($this->uid, $ids);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="购物车产品修改数量", path="/shop/numChangeCart", version="1.0.0")
     * @ApiDescription(value="购物车产品修改数量")
     * @Method(allow={POST})
     * @Param(name="id", description="id", required="", notEmpty="", from={JSON})
     * @Param(name="num", description="数量", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function numChangeCart($id, $num): bool
    {
        $res = ShopService::getInstance()->numChangeCart($this->uid, $id, $num);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="推荐产品列表", path="/shop/recommendGoods", version="1.0.0")
     * @ApiDescription(value="推荐产品列表")
     * @Method(allow={POST})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function recommendGoods($limit, $page): bool
    {
        $res = ShopService::getInstance()->recommendGoods($limit, $page);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取我的订单", path="/shop/getOrderList", version="1.0.0")
     * @ApiDescription(value="获取我的订单")
     * @Method(allow={POST})
     * @Param(name="status", defaultValue=0, description="订单状态：0，1，,2，,3（待付款，待发货，待收货， 待评价）", from={JSON})
     * @Param(name="limit", defaultValue=10, description="单页数量", from={JSON})
     * @Param(name="page", defaultValue=1, description="页数", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getOrderList($status, $limit, $page): bool
    {
        $res = ShopService::getInstance()->getOrderList($this->uid, $status, $limit, $page);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取订单详情", path="/shop/getOrderDetail", version="1.0.0")
     * @ApiDescription(value="获取订单详情")
     * @Method(allow={POST})
     * @Param(name="oid", description="订单id", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function getOrderDetail($oid): bool
    {
        $res = ShopService::getInstance()->getOrderDetail($this->uid, $oid);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="商品下单", path="/shop/createOrder", version="1.0.0")
     * @ApiDescription(value="商品下单")
     * @Method(allow={POST})
     * @Param(name="goods", description="下单商品数组[{'goods_sku' => 'goods_sku','num' => 1}]", required="", notEmpty="", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     */
    public function createOrder($goods): bool
    {
        $res = ShopService::getInstance()->createOrder($this->uid, $goods);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="确认订单", path="/shop/confirmOrder", version="1.0.0")
     * @ApiDescription(value="确认订单")
     * @Method(allow={POST})
     * @Param(name="order_no", description="订单号", required="", notEmpty="", from={JSON})
     * @Param(name="truck_type", description="物流配送类型(0无需配送,1需要配送)", required="", notEmpty="", from={JSON})
     * @Param(name="aid", description="地址id", from={JSON})
     * @Param(name="order_remark", description="订单用户备注", from={JSON})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function confirmOrder($order_no, $truck_type, $order_remark,$aid): bool
    {
        $ips = $this->request()->getHeaders()['x-real-ip'] ?? [];
        $this->ip = $ips[0] ?? '127.0.0.1';
        $res = ShopService::getInstance()->confirmOrder($this->uid, $order_no, $truck_type, $order_remark,$aid, $this->ip);
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }

    /**
     * @Api(name="获取首页商城导航", path="/shop/getHomeShopNav", version="1.0.0")
     * @ApiDescription(value="获取首页商城导航")
     * @Method(allow={POST})
     * @return bool
     * @throws Exception
     * @throws Throwable
     * @throws \EasySwoole\Mysqli\Exception\Exception
     */
    public function getHomeShopNav(): bool
    {
        $res = ShopService::getInstance()->getHomeShopNav();
        if (!$res['result']) {
            return $this->writeJsonError($res['msg']);
        }
        return $this->writeJsonSuccess($res['data'], $res['msg']);
    }
}